package jp.co.piratealliance.pirateAllianceWeb.web.action.chat;

import java.io.IOException;

import jp.co.piratealliance.pirateAllianceWeb.common.base.BaseAction;
import jp.co.piratealliance.pirateAllianceWeb.def.SystemConf;
import jp.co.piratealliance.pirateAllianceWeb.dto.com.ResultDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.com.UserInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.helper.XMPPHelper;
import jp.co.piratealliance.pirateAllianceWeb.service.com.UserService;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

/**
 * copyright   pirate alliance team 2014
 * @author     xuwei
 *
 */
public class FirendAction extends BaseAction
{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 用户id
	 */
	private String userId;

	/**
	 * 好友ID
	 */
	private String firendUserId;

	/**
	 * 结果
	 */
	private ResultDto result;

	private UserService userService;

	/**
	 * 追加好友
	 * @return
	 */
	public String add()
	{
		UserInfoDto userInfo = this.userService.getServiceUserInfo(userId);

		UserInfoDto firUserInfo = this.userService.getServiceUserInfo(firendUserId);

		boolean result1 = this.addFirend(userId, userInfo.getPassword(), firendUserId, firUserInfo.getName_kanji_mei(),SystemConf.getXMPPDomain());

		boolean result2 = this.addFirend(firendUserId, firUserInfo.getPassword(), userId, userInfo.getName_kanji_mei(), SystemConf.getXMPPDomain());

		result = new ResultDto();

		if(result1 && result2)
		{
			result.setResult(true);
		}
		else
		{
			result.setResult(false);
		}

		return SUCCESS;
	}

	private boolean addFirend(String userId,String password, String firendUserId, String nickName,String domain)
	{
		XMPPConnection connection = null;
		try
		{
			connection = XMPPHelper.getConnection(domain);
			connection.connect();

			connection.login(userId, password);

			XMPPHelper.addFirend(connection, firendUserId + "@" + domain, nickName);

		}
		catch (XMPPException | SmackException | IOException e)
		{
			e.printStackTrace();
			return false;
		}
		catch(Error e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			if(connection != null)
			{
				try
				{
					connection.disconnect();
				}
				catch (NotConnectedException e)
				{

				}
			}
		}

		return true;
	}

	/**
	 * @return the userId
	 */
	public String getUserId()
	{
		return userId;
	}

	/**
	 * @param userId the userId to set
	 */
	public void setUserId(String userId)
	{
		this.userId = userId;
	}

	/**
	 * @return the firendUserId
	 */
	public String getFirendUserId()
	{
		return firendUserId;
	}

	/**
	 * @param firendUserId the firendUserId to set
	 */
	public void setFirendUserId(String firendUserId)
	{
		this.firendUserId = firendUserId;
	}

	/**
	 * @return the result
	 */
	public ResultDto getResult()
	{
		return result;
	}

	/**
	 * @param result the result to set
	 */
	public void setResult(ResultDto result)
	{
		this.result = result;
	}

	/**
	 * @return the userService
	 */
	public UserService getUserService()
	{
		return userService;
	}

	/**
	 * @param userService the userService to set
	 */
	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}
}
